﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FraudMitigator
{
    public partial class AddCheckForm : Form
    {
        private TextBox t;

        // TODO: Add validation checks
        //          - Error if user hasn't entered text
        //          - Check length of routing and account numbers
        //          - Verify that check number is a number

        public AddCheckForm()
        {
            InitializeComponent();
        }

        private void setTextBox(object s) { t = (TextBox)s; }

        private void eraseText(object sender)
        {
            setTextBox(sender);
            t.Text = "";
        }

        private void firstnameTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (t.Text == "First") eraseText(sender);
        }

        private void lastnameTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (t.Text == "Last") eraseText(sender);
        }

        private void addrTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (addrTxtBox.Text == "Street Address") eraseText(sender);
        }

        private void cityTxtBox_MouseClicked(object sender, EventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (cityTxtBox.Text == "City") eraseText(sender);
        }

        private void zipTxtBox_MouseClicked(object sender, EventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (zipTxtBox.Text == "Zip") eraseText(sender);
        }

        private void routingTxtBox_MouseClicked(object sender, EventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (routingTxtBox.Text == "Routing Number") eraseText(sender);
        }

        private void accountTxtBox_MouseClicked(object sender, EventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (accountTxtBox.Text == "Account Number") eraseText(sender);
        }

        private void amountTxtBox_MouseClicked(object sender, EventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (amountTxtBox.Text == "Amount") eraseText(sender);
        }

        private void checkNumTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (checkNumTxtBox.Text == "Check Number") eraseText(sender);
        }

        private void checkNumTxtBox2_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (checkNumTxtBox2.Text == "Check Number") eraseText(sender);
        }

        private void bankNameTxtBox_MouseClick(object sender, MouseEventArgs e)
        {
            setTextBox(sender);
            checkBlanks();
            if (bankNameTxtBox.Text == "Bank Name") eraseText(sender);
        }

        private void checkNumTxtBox_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            checkNumTxtBox2.Text = t.Text;
        }

        private void checkNumTxtBox2_TextChanged(object sender, EventArgs e)
        {
            TextBox t = (TextBox)sender;
            checkNumTxtBox.Text = t.Text;
        }

        private void checkBlanks()
        {
            if (firstnameTxtBox.Text == "") firstnameTxtBox.Text = "First";
            if (lastnameTxtBox.Text == "") lastnameTxtBox.Text = "Last";
            if (addrTxtBox.Text == "") addrTxtBox.Text = "Street Address";
            if (cityTxtBox.Text == "") cityTxtBox.Text = "City";
            if (zipTxtBox.Text == "") zipTxtBox.Text = "Zip";
            if (checkNumTxtBox.Text == "") { checkNumTxtBox.Text = "Check Number"; checkNumTxtBox2.Text = "Check Number"; }
            if (routingTxtBox.Text == "") routingTxtBox.Text = "Routing Number";
            if (accountTxtBox.Text == "") accountTxtBox.Text = "Account Number";
            if (amountTxtBox.Text == "") amountTxtBox.Text = "Amount";
            if (bankNameTxtBox.Text == "") bankNameTxtBox.Text = "Bank Name";
        }

        private void addBTN_MouseClicked(object sender, EventArgs e)
        {
            if (firstnameTxtBox.Text == "" || firstnameTxtBox.Text == "First") firstnameTxtBox.Focus();
            else if (lastnameTxtBox.Text == "" || lastnameTxtBox.Text == "Last") lastnameTxtBox.Focus();
            else if (addrTxtBox.Text == "" || addrTxtBox.Text == "Street Address") addrTxtBox.Focus();
            else if (cityTxtBox.Text == "" || cityTxtBox.Text == "City") cityTxtBox.Focus();
            else if (zipTxtBox.Text == "" || zipTxtBox.Text == "Zip") zipTxtBox.Focus();
            else if (checkNumTxtBox.Text == "" || checkNumTxtBox.Text == "Check Number") checkNumTxtBox.Focus();
            else if (checkNumTxtBox2.Text == "" || checkNumTxtBox2.Text == "Check Number") checkNumTxtBox2.Focus();
            else if (routingTxtBox.Text == "" || routingTxtBox.Text == "Routing Number") routingTxtBox.Focus();
            else if (accountTxtBox.Text == "" || accountTxtBox.Text == "Account Number") accountTxtBox.Focus();
            else if (amountTxtBox.Text == "" || amountTxtBox.Text == "Amount") amountTxtBox.Focus();
            else if (stateComboBox.Text == "State") stateComboBox.Focus();
            else if (bankNameTxtBox.Text == "Bank Name") bankNameTxtBox.Focus();
            else
            {
                if (!(InputValidator.validateText("name", firstnameTxtBox.Text)))
                {
                    firstnameTxtBox.Select();
                    MessageBox.Show("First Name does not have valid data.");
                }
                else if (!(InputValidator.validateText("name", lastnameTxtBox.Text)))
                {
                    lastnameTxtBox.Select();
                    MessageBox.Show("Last Name does not have valid data.");
                }
                else if (!(InputValidator.validateText("address", addrTxtBox.Text)))
                {
                    addrTxtBox.Select();
                    MessageBox.Show("Street Address does not have valid data.");
                }
                else if (!(InputValidator.validateText("city", cityTxtBox.Text)))
                {
                    cityTxtBox.Select();
                    MessageBox.Show("City does not have valid data.");
                }
                else if (!(InputValidator.validateText("zip", zipTxtBox.Text)))
                {
                    zipTxtBox.Select();
                    MessageBox.Show("Zip code invalid.");
                }
                else if (!(InputValidator.validateText("integer", checkNumTxtBox.Text)))
                {
                    checkNumTxtBox.Select();
                    MessageBox.Show("Invalid check number.");
                }
                else if (!(InputValidator.validateText("integer", routingTxtBox.Text)))
                {
                    routingTxtBox.Select();
                    MessageBox.Show("Invalid routing number.");
                }
                else if (!(InputValidator.validateText("integer", accountTxtBox.Text)))
                {
                    accountTxtBox.Select();
                    MessageBox.Show("Invalid account number.");
                }
                else if (!(InputValidator.validateText("currency", amountTxtBox.Text)))
                {
                    amountTxtBox.Select();
                    MessageBox.Show("Invalid amount.");
                }
                else if (!(InputValidator.validateText("state", stateComboBox.Text)))
                {
                    stateComboBox.Select();
                    MessageBox.Show("Invalid state. Please use only uppercase.");
                }
                else if (!(InputValidator.validateText("city", bankNameTxtBox.Text)))
                {
                    bankNameTxtBox.Select();
                    MessageBox.Show("Invalid bank name.");
                }

                else
                {
                    CheckData data = new CheckData();
                    data.firstName = firstnameTxtBox.Text;
                    data.lastName = lastnameTxtBox.Text;
                    data.address = addrTxtBox.Text;
                    data.city = cityTxtBox.Text;
                    data.state = stateComboBox.Text;
                    data.zip = zipTxtBox.Text;
                    data.accountNum = accountTxtBox.Text;
                    data.routingNum = routingTxtBox.Text;
                    data.checkNum = checkNumTxtBox.Text;
                    data.date = dateTxtBox.Text;
                    data.bankName = bankNameTxtBox.Text;
                    data.amount = amountTxtBox.Text;

                    Queries.addCheckQuery(data);
                Strings.Log(MainScreen.user.UserUsername + " added a check.");
                }
            }
        }
    }

    public struct CheckData
    {
        public string date { get; set; }
        public string lastName { get; set; }
        public string firstName { get; set; }
        public string address { get; set; }
        public string city { get; set; }
        public string state { get; set; }
        public string zip { get; set; }
        public string amount { get; set; }
        public string checkNum { get; set; }
        public string accountNum { get; set; }
        public string routingNum { get; set; }
        public string bankName { get; set; }
    }
}
